﻿// -----------------------------------------------------------------------
// <copyright file="Document_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Certificate_DAL:BaseDAL
    {
        
        // Lấy tất cả Certificate
        public static List<Certificate> GetAll()
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.Certificates);
            return DB.Certificates.ToList();
        }
        public static int GetQuantityCertificateDue(TypeCertificate certificateType)
        {
            return DB.Certificates.Where(n => n.TypeCertificate_ID == certificateType.TypeCertificate_ID && n.ExpirationDate.Value.AddDays(n.DateAlarmBefore.Value).Date.CompareTo(DateTime.Now.Date) <= 0).Count();
        }
        public static List<Certificate> GetCertificateOverDueList(int type)
        {
            return DB.Certificates.Where(n => n.TypeCertificate_ID == type && n.ExpirationDate.Value.Date.CompareTo(DateTime.Now.Date) <= 0).ToList();
        }
        // Lấy Certificate theo Id
        public static Certificate GetDataById(int id)
        {
            return DB.Certificates.Where(m => m.Certificate_ID == id).FirstOrDefault();
        }
        //Thêm một Certificate
        public static bool Insert(Certificate Certificate)
        {
            try
            {
                DB.Certificates.InsertOnSubmit(Certificate);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Xóa một Certificate
        public static bool Delete(Certificate Certificate)
        {
            try
            {
                DB.Certificates.DeleteOnSubmit(Certificate);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Cập nhật Certificate
        public static bool Update(Certificate Certificate)
        {
            try
            {
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }

        // Kiểm tra xem Certificate Code có tồn tại chưa s
        public static bool IsExits(Certificate Certificate)
        {
            Certificate objCertificate = null;
            objCertificate = DB.Certificates.Where(m => (m.CodeCertificate == Certificate.CodeCertificate) &&
                (m.Certificate_ID != Certificate.Certificate_ID) && (m.TypeCertificate.Ship_ID == Certificate.TypeCertificate.Ship_ID)).FirstOrDefault();
            return objCertificate != null ? true : false;
        }
        public static List<Certificate> GetDataByShipID(int id, bool isChanged)
        {
            if (isChanged)
            {
                DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.Certificates);
            }
            return DB.Certificates.Where(j => j.TypeCertificate.Ship_ID == id).ToList();
        }
    }
}
